﻿using System;
using System.Collections;
using System.Collections.Generic;
using Me.Prettyprint.Cassandra.Connection;
using Me.Prettyprint.Cassandra.Serializers;
using Me.Prettyprint.Cassandra.Service;
using Me.Prettyprint.Cassandra.Utils;
using Me.Prettyprint.Hector.Api;
using Me.Prettyprint.Hector.Api.Beans;
using Me.Prettyprint.Hector.Api.Query;
using Org.Apache.Cassandra.Thrift;

namespace Me.Prettyprint.Cassandra.Model
{
    public class ConfigurableConsistencyLevel : ConsistencyLevelPolicy
    {
        protected Dictionary<string, HConsistencyLevel> readCfConsistencyLevels = new Dictionary<string, HConsistencyLevel>();
        protected Dictionary<string, HConsistencyLevel> writeCfConsistencyLevels = new Dictionary<string, HConsistencyLevel>();
        protected HConsistencyLevel defaultReadConsistencyLevel = HConsistencyLevel.QUORUM;
        protected HConsistencyLevel defaultWriteConsistencyLevel = HConsistencyLevel.QUORUM;
        public HConsistencyLevel Get(OperationType op)
        {
            return (op == OperationType.READ) ? defaultReadConsistencyLevel : defaultWriteConsistencyLevel;
        }
        public HConsistencyLevel Get(OperationType op, String cfName)
        {
            if (op == OperationType.READ)
            {
                HConsistencyLevel rcf = readCfConsistencyLevels[cfName];
                return rcf != null ? rcf : defaultReadConsistencyLevel;
            }
            else
            {
                HConsistencyLevel wcf = writeCfConsistencyLevels[cfName];
                return wcf != null ? wcf : defaultWriteConsistencyLevel;
            }
        }
        public void SetReadCfConsistencyLevels(Dictionary<string, HConsistencyLevel> columnFamilyConsistencyLevels)
        {
            this.readCfConsistencyLevels = columnFamilyConsistencyLevels;
        }

        public void SetWriteCfConsistencyLevels(Dictionary<string, HConsistencyLevel> columnFamilyConsistencyLevels)
        {
            this.writeCfConsistencyLevels = columnFamilyConsistencyLevels;
        }
        public void setConsistencyLevelForCfOperation(HConsistencyLevel consistencyLevel, string columnFamily,
            OperationType operationType)
        {
            if (operationType == OperationType.READ)
            {
                readCfConsistencyLevels[columnFamily] = consistencyLevel;
            }
            else
            {
                writeCfConsistencyLevels[columnFamily] = consistencyLevel;
            }
        }

        public void setDefaultReadConsistencyLevel(HConsistencyLevel defaultReadConsistencyLevel)
        {
            this.defaultReadConsistencyLevel = defaultReadConsistencyLevel;
        }

        public void setDefaultWriteConsistencyLevel(HConsistencyLevel defaultWriteConsistencyLevel)
        {
            this.defaultWriteConsistencyLevel = defaultWriteConsistencyLevel;
        }

    }
}
